package com.sskj.one.mine;

import android.content.Context;
import android.content.Intent;
import android.media.audiofx.Visualizer;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.test.mock.MockContentResolver;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.bulong.rudeness.RudenessScreenHelper;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.lib.adapter.BaseAdapter;
import com.sskj.lib.adapter.ViewHolder;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.mvchelper.ModelRx2DataSource;
import com.sskj.lib.mvchelper.SmartRefreshDataSource;
import com.sskj.lib.mvchelper.refresh.MVCSmartHelper;
import com.sskj.one.R;
import com.sskj.one.bean.GonggaoBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Flowable;
import io.reactivex.Observable;

/**
 * @author Hey
 * Create at  2019/05/22
 */
public class GongGaoActivity extends BaseActivity<GongGaoPresenter> {


    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.smartRefreshView)
    SmartRefreshLayout smartRefreshView;

    private BaseAdapter<GonggaoBean> adapter;
    private int page = 1;
    private int size = 10;


    @Override
    public int getLayoutId() {
        return R.layout.app_activity_gong_gao;
    }

    @Override
    public GongGaoPresenter getPresenter() {
        return new GongGaoPresenter();
    }

    @Override
    public void initView() {

        tvTitle.setText("平台公告");

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(this)
                .setLeftPadding((int) RudenessScreenHelper.dp2px(this, 15))
                .setFirstDraw(false)
                .setLastDraw(false)
                .setRightPadding((int) RudenessScreenHelper.dp2px(this, 15))
                .setPaintWidth((int) RudenessScreenHelper.dp2px(this, 1))
                .setPaintColor(ContextCompat.getColor(this, R.color.divide)));
        adapter = new BaseAdapter<GonggaoBean>(R.layout.item_gonggao, null, recyclerView, true) {
            @Override
            public void bind(ViewHolder holder, GonggaoBean item) {
                holder.setText(R.id.tv_title, item.getTitle())
                        .setOnClickListener(R.id.gonggao, v -> DetailGonggaoActivity.start(GongGaoActivity.this, item.getId()))
                        .setText(R.id.tv_date, item.getCreate_time());
            }
        };
    }

    @Override
    public void initData() {
        mPresenter.getGonggaoList(true, page, size);
        smartRefreshView.setOnRefreshListener(refreshLayout -> mPresenter.getGonggaoList(false, page, size));
        smartRefreshView.setOnLoadMoreListener(refreshLayout -> mPresenter.getGonggaoList(false, page, size));
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, GongGaoActivity.class);
        context.startActivity(intent);
    }

    public void getGonggaoListOK(List<GonggaoBean> data) {
        if (data == null) {
            return;
        }
        if (page == 1) {
            adapter.setNewData(data);
        } else {
            adapter.addData(data);
        }
    }

    public void stopRefresh() {
        smartRefreshView.finishRefresh();
        smartRefreshView.finishLoadMore();
    }
}
